﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using bdnt_pets_system_Entity;
using System.Linq;
using System.Globalization;
using bdnt_pets_system.Tools;
using System.Collections.Generic;

namespace bdnt_pets_system.MyUserControls
{
    public class DictComboBox : ComboBox
    {
        bool _HasAll = false;
        public bool HasAll
        {
            get { return _HasAll; }
            set { _HasAll = value; }
        }

        List<Tbl_DictInfo> itemS;
        public List<Tbl_DictInfo> ItemS
        {
            get { return itemS; }
            set { itemS = value; }
        }

        string _DefalutStr = "";
        public string DefalutStr
        {
            get { return _DefalutStr; }
            set { _DefalutStr = value; }
        }

        int _Pid = 0;
        public int Pid
        {
            get { return _Pid; }
            set { _Pid = value; }
        }

        public string CurrentField { set; get; }

        public DictComboBox()
            : base()
        {
            DictComboBox_Loaded();
            //this.Loaded += new RoutedEventHandler(DictComboBox_Loaded);
        }

        //void sysClient_GetDictListByPidCompleted(object sender, SystemServiceReference.GetDictListByPidCompletedEventArgs e)
        //{
        //    var result = e.Result.ToList();

        //    if (_HasAll == true)
        //    {
        //        result.Insert(0, new Tbl_DictInfo()
        //        {
        //            DictName = "全部",
        //            Id = 0,
        //            DictValue = "0",
        //            Sequence = 0
        //        });
        //    }
        //    if (!string.IsNullOrEmpty(_DefalutStr))
        //    {
        //        result.Insert(0, new Tbl_DictInfo()
        //        {
        //            DictName = _DefalutStr,
        //            Id = -1,
        //            DictValue = "-1",
        //            Sequence = -1
        //        });
        //    }
        //    base.ItemsSource = result;

        //    if (this.DataContext != null && !string.IsNullOrEmpty(CurrentField))
        //    {
        //        PropertyInfo pi = this.DataContext.GetType().GetProperty(CurrentField);
        //        if (pi == null)
        //            return;
        //        object value = pi.GetValue(this.DataContext, null);
        //        if (value != null)
        //        {
        //            foreach (object obj in result)
        //            {
        //                object dictvalue = obj.GetType().GetProperty(this.SelectedValuePath).GetValue(obj, null);
        //                if (dictvalue.Equals(value.ToString()) || dictvalue == value)
        //                {
        //                    int index = result.IndexOf(obj as Tbl_DictInfo);
        //                    this.SelectedIndex = index;                         
        //                    break;
        //                }
        //            }
        //        }
        //    }
        //}
        void DictComboBox_Loaded()
        {

            base.DisplayMemberPath = SessionManager.IsCN_ZH ? "DictName" : "en_DictName";
            base.SelectedValuePath = "DictValue";

            var res = this.itemS;
            if (res == null)
            {
                return;
            }
            this.ItemsSource = res;
            if (this.DataContext != null && !string.IsNullOrEmpty(CurrentField))
            {
                PropertyInfo pi = this.DataContext.GetType().GetProperty(CurrentField);
                if (pi == null)
                    return;
                object value = pi.GetValue(this.DataContext, null);
                if (value != null)
                {
                    foreach (object obj in res)
                    {
                        object dictvalue = obj.GetType().GetProperty(this.SelectedValuePath).GetValue(obj, null);
                        if (dictvalue.Equals(value.ToString()) || dictvalue == value)
                        {
                            int index = res.IndexOf(obj as Tbl_DictInfo);
                            this.SelectedIndex = index;
                            break;
                        }
                    }
                }
            }

            //if (this.ItemsSource == null)
            //{

            //    ////菜单类型
            //    //if (DictInfoManager.LoadDictsByPId(Pid))
            //    //{
            //    //    var cuos = DictInfoManager.CurrentDicts.ToList();
            //    //    base.ItemsSource = cuos;
            //    //}
            //    //else
            //    //{
            //    //    SystemServiceReference.SystemServiceClient sysClient = new SystemServiceReference.SystemServiceClient();
            //    //    sysClient.GetDictListByPidCompleted += new EventHandler<SystemServiceReference.GetDictListByPidCompletedEventArgs>(sysClient_GetDictListByPidCompleted);
            //    //    sysClient.GetDictListByPidAsync(Pid);
            //    //    sysClient.CloseAsync();
            //    //}

            //}
        }
        void DictComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (CultureInfo.CurrentCulture.Name == "en-US")
                base.DisplayMemberPath = "en_DictName";
            else
                base.DisplayMemberPath = "DictName";
            base.SelectedValuePath = "DictValue";

            var res = this.itemS;
            if (res == null)
            {
                return;
            }
            this.ItemsSource = res;
            if (this.DataContext != null && !string.IsNullOrEmpty(CurrentField))
            {
                PropertyInfo pi = this.DataContext.GetType().GetProperty(CurrentField);
                if (pi == null)
                    return;
                object value = pi.GetValue(this.DataContext, null);
                if (value != null)
                {
                    foreach (object obj in res)
                    {
                        object dictvalue = obj.GetType().GetProperty(this.SelectedValuePath).GetValue(obj, null);
                        if (dictvalue.Equals(value.ToString()) || dictvalue == value)
                        {
                            int index = res.IndexOf(obj as Tbl_DictInfo);
                            this.SelectedIndex = index;
                            break;
                        }
                    }
                }
            }

            //if (this.ItemsSource == null)
            //{

            //    ////菜单类型
            //    //if (DictInfoManager.LoadDictsByPId(Pid))
            //    //{
            //    //    var cuos = DictInfoManager.CurrentDicts.ToList();
            //    //    base.ItemsSource = cuos;
            //    //}
            //    //else
            //    //{
            //    //    SystemServiceReference.SystemServiceClient sysClient = new SystemServiceReference.SystemServiceClient();
            //    //    sysClient.GetDictListByPidCompleted += new EventHandler<SystemServiceReference.GetDictListByPidCompletedEventArgs>(sysClient_GetDictListByPidCompleted);
            //    //    sysClient.GetDictListByPidAsync(Pid);
            //    //    sysClient.CloseAsync();
            //    //}

            //}
        }
    }
}
